package com.abbcc.module.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.abbcc.action.BaseAction;
import com.abbcc.models.TdProduct;
import com.abbcc.service.ProductService;
import com.abbcc.util.DateUtil;
import com.abbcc.util.StringUtil;

public class StatAction extends BaseAction<TdProduct> {
	private String dd1;
	private String dd2;
	private int yearNumber;
	@Autowired
	private ProductService productService;

	public String list() {
		// deposit("dmCount", sumByPace("单门", ""));
		deposit("dmbjCount", sumByPace("单门", "板金"));
		deposit("dmtzCount", sumByPace("单门", "涂装"));
		deposit("dmzpCount", sumByPace("单门", "装配"));
		deposit("dmfhCount", sumByPace("单门", "发货"));
		// deposit("dmfbCount", sumByPace("单门封闭", ""));
		deposit("dmfbbjCount", sumByPace("单门封闭", "板金"));
		deposit("dmfbtzCount", sumByPace("单门封闭", "涂装"));
		deposit("dmfbzpCount", sumByPace("单门封闭", "装配"));
		deposit("dmfbfhCount", sumByPace("单门封闭", "发货"));
		// deposit("zmCount", sumByPace("子母", ""));
		deposit("zmbjCount", sumByPace("子母", "板金"));
		deposit("zmtzCount", sumByPace("子母", "涂装"));
		deposit("zmzpCount", sumByPace("子母", "装配"));
		deposit("zmfhCount", sumByPace("子母", "发货"));
		// deposit("zmfbCount", sumByPace("子母封闭", ""));
		deposit("zmfbbjCount", sumByPace("子母封闭", "板金"));
		deposit("zmfbtzCount", sumByPace("子母封闭", "涂装"));
		deposit("zmfbzpCount", sumByPace("子母封闭", "装配"));
		deposit("zmfbfhCount", sumByPace("子母封闭", "发货"));
		// deposit("dkCount", sumByPace("对开", ""));
		deposit("dkbjCount", sumByPace("对开", "板金"));
		deposit("dktzCount", sumByPace("对开", "涂装"));
		deposit("dkzpCount", sumByPace("对开", "装配"));
		deposit("dkfhCount", sumByPace("对开", "发货"));
		// deposit("dkfbCount", sumByPace("对开封闭", ""));
		deposit("dkfbbjCount", sumByPace("对开封闭", "板金"));
		deposit("dkfbtzCount", sumByPace("对开封闭", "涂装"));
		deposit("dkfbzpCount", sumByPace("对开封闭", "装配"));
		deposit("dkfbfhCount", sumByPace("对开封闭", "发货"));
		// deposit("skCount", sumByPace("四开", ""));
		deposit("skbjCount", sumByPace("四开", "板金"));
		deposit("sktzCount", sumByPace("四开", "涂装"));
		deposit("skzpCount", sumByPace("四开", "装配"));
		deposit("skfhCount", sumByPace("四开", "发货"));
		// deposit("skfbCount", sumByPace("四开封闭", ""));
		deposit("skfbbjCount", sumByPace("四开封闭", "板金"));
		deposit("skfbtzCount", sumByPace("四开封闭", "涂装"));
		deposit("skfbzpCount", sumByPace("四开封闭", "装配"));
		deposit("skfbfhCount", sumByPace("四开封闭", "发货"));
		// deposit("skzmCount", sumByPace("四开子母", ""));
		deposit("skzmbjCount", sumByPace("四开子母", "板金"));
		deposit("skzmtzCount", sumByPace("四开子母", "涂装"));
		deposit("skzmzpCount", sumByPace("四开子母", "装配"));
		deposit("skzmfhCount", sumByPace("四开子母", "发货"));
		// deposit("skzmfbCount", sumByPace("四开子母封闭", ""));
		deposit("skzmfbbjCount", sumByPace("四开子母封闭", "板金"));
		deposit("skzmfbtzCount", sumByPace("四开子母封闭", "涂装"));
		deposit("skzmfbzpCount", sumByPace("四开子母封闭", "装配"));
		deposit("skzmfbfhCount", sumByPace("四开子母封闭", "发货"));

		return LIST;
	}

	private Object sumByPace(String name, String pace) {
		String time;
		if (pace.equals("发货"))
			time = "fhTime";
		else
			time = "addTime";
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (StringUtil.isNotBlank(dd1) && StringUtil.isNotBlank(dd2)) {
			if (StringUtil.isBlank(pace)) {
				dc.add(Restrictions.or(Restrictions.between("fhTime",
						DateUtil.getDateStart(DateUtil.strToDate(dd1)),
						DateUtil.getDateEnd(DateUtil.strToDate(dd2))),
						Restrictions.between("addTime",
								DateUtil.getDateStart(DateUtil.strToDate(dd1)),
								DateUtil.getDateEnd(DateUtil.strToDate(dd2)))));
			} else {
				dc.add(Restrictions.between(time,
						DateUtil.getDateStart(DateUtil.strToDate(dd1)),
						DateUtil.getDateEnd(DateUtil.strToDate(dd2))));
			}
		} else if (StringUtil.isNotBlank(dd1)) {
			if (StringUtil.isBlank(pace)) {
				dc.add(Restrictions.or(Restrictions.between("fhTime",
						DateUtil.getDateStart(DateUtil.strToDate(dd1)),
						new Date()), Restrictions.between("addTime",
						DateUtil.getDateStart(DateUtil.strToDate(dd1)),
						new Date())));
			} else {
				dc.add(Restrictions.between(time,
						DateUtil.getDateEnd(DateUtil.strToDate(dd1)),
						new Date()));
			}
		} else if (StringUtil.isNotBlank(dd2)) {
			if (StringUtil.isBlank(pace)) {
				dc.add(Restrictions.or(Restrictions.between("fhTime",
						DateUtil.getDateStart(DateUtil.strToDate(dd2)),
						DateUtil.getDateEnd(DateUtil.strToDate(dd2))),
						Restrictions.between("addTime",
								DateUtil.getDateStart(DateUtil.strToDate(dd2)),
								DateUtil.getDateEnd(DateUtil.strToDate(dd2)))));
			} else {
				dc.add(Restrictions.between(time,
						DateUtil.getDateStart(DateUtil.strToDate(dd2)),
						DateUtil.getDateEnd(DateUtil.strToDate(dd2))));
			}

		} else {

		}
		if (StringUtil.isNotBlank(name)) {
			dc.add(Restrictions.eq("name", name));
		}
		if (StringUtil.isNotBlank(pace)) {
			if (pace.equals("板金"))
				dc.add(Restrictions.or(Restrictions.isNull("pace"),
						Restrictions.eq("pace", pace)));
			else
				dc.add(Restrictions.eq("pace", pace));
		}
		dc.add(Restrictions.eq("state", "1"));
		dc.setProjection(Projections.sum("proCount"));

		List t = productService.findAllByCriteria(dc);
		if (t.isEmpty())
			return 0;
		return t.get(0) == null ? 0 : t.get(0);
	}


	public String fhStat() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.add(Restrictions.eq("pace", "发货"));
		dc.add(Restrictions.isNotNull("fhTime"));
		dc.add(Restrictions.eq("state", "1"));
		List<TdProduct> list = baseService.findAllByCriteria(dc);
		DateTime dt = new DateTime();
		int thisYear = 0;
		if(yearNumber!=0)
			thisYear = yearNumber;
		else{
			dt = new DateTime(new Date());
			thisYear = dt.getYear();
		}
		Map mapdm = new HashMap();
		Map mapdmfb = new HashMap();
		Map mapsk = new HashMap();
		Map mapskfb = new HashMap();
		Map mapzm = new HashMap();
		Map mapzmfb = new HashMap();
		Map mapskzm = new HashMap();
		Map mapskzmfb = new HashMap();
		Map mapdk = new HashMap();
		Map mapdkfb = new HashMap();
		int[] dm = new int[12];
		int[] dmfb = new int[12];
		int[] zm = new int[12];
		int[] zmfb = new int[12];
		int[] sk = new int[12];
		int[] skfb = new int[12];
		int[] dk = new int[12];
		int[] dkfb = new int[12];
		int[] skzm = new int[12];
		int[] skzmfb = new int[12];
		for (TdProduct au : list) {
			DateTime dtt = new DateTime(au.getFhTime());
			if (dtt.getYear() == thisYear) {
				if(au.getName().equals("单门")){
					if (dm[dtt.getMonthOfYear() - 1] == 0) {
						dm[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						dm[dtt.getMonthOfYear() - 1] = dm[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("单门封闭")){
					if (dmfb[dtt.getMonthOfYear() - 1] == 0) {
						dmfb[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						dmfb[dtt.getMonthOfYear() - 1] = dmfb[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("对开")){
					if (dk[dtt.getMonthOfYear() - 1] == 0) {
						dk[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						dk[dtt.getMonthOfYear() - 1] = dk[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("对开封闭")){
					if (dkfb[dtt.getMonthOfYear() - 1] == 0) {
						dkfb[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						dkfb[dtt.getMonthOfYear() - 1] = dkfb[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("四开")){
					if (sk[dtt.getMonthOfYear() - 1] == 0) {
						sk[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						sk[dtt.getMonthOfYear() - 1] = sk[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("四开封闭")){
					if (skfb[dtt.getMonthOfYear() - 1] == 0) {
						skfb[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						skfb[dtt.getMonthOfYear() - 1] = skfb[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("子母")){
					if (zm[dtt.getMonthOfYear() - 1] == 0) {
						zm[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						zm[dtt.getMonthOfYear() - 1] = zm[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("子母封闭")){
					if (zmfb[dtt.getMonthOfYear() - 1] == 0) {
						zmfb[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						zmfb[dtt.getMonthOfYear() - 1] = zmfb[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("四开子母")){
					if (skzm[dtt.getMonthOfYear() - 1] == 0) {
						skzm[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						skzm[dtt.getMonthOfYear() - 1] = skzm[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
				if(au.getName().equals("四开子母封闭")){
					if (skzmfb[dtt.getMonthOfYear() - 1] == 0) {
						skzmfb[dtt.getMonthOfYear() - 1] = au.getProCount();
					} else {
						skzmfb[dtt.getMonthOfYear() - 1] = skzmfb[dtt
								.getMonthOfYear() - 1] + au.getProCount();
					}
				}
			}
			
		}
		mapdm.put("name", thisYear+"年,单门");
		mapdm.put("data", dm);
		mapdmfb.put("name", thisYear+"年,单门封闭");
		mapdmfb.put("data", dmfb);
		mapsk.put("name", thisYear+"年,四开");
		mapsk.put("data", sk);
		mapskfb.put("name", thisYear+"年,四开封闭");
		mapskfb.put("data", skfb);
		mapdk.put("name", thisYear+"年,对开");
		mapdk.put("data", dk);
		mapdkfb.put("name", thisYear+"年,对开封闭");
		mapdkfb.put("data", dkfb);
		mapzm.put("name", thisYear+"年,子母");
		mapzm.put("data", zm);
		mapzmfb.put("name", thisYear+"年,子母封闭");
		mapzmfb.put("data", zmfb);
		mapskzm.put("name", thisYear+"年,四开子母");
		mapskzm.put("data", skzm);
		mapskzmfb.put("name", thisYear+"年,四开子母封闭");
		mapskzmfb.put("data", skzmfb);

		Map[] m = { mapdm, mapdmfb, mapsk,mapskfb, mapdk,mapdkfb,mapzm,mapzmfb,mapskzm,mapskzmfb};
		JSONArray json = JSONArray.fromObject(m);
		result = json.toString();
		return JSON;
	}

	public String getDd1() {
		return dd1;
	}

	public void setDd1(String dd1) {
		this.dd1 = dd1;
	}

	public String getDd2() {
		return dd2;
	}

	public void setDd2(String dd2) {
		this.dd2 = dd2;
	}

	public int getYearNumber() {
		return yearNumber;
	}

	public void setYearNumber(int yearNumber) {
		this.yearNumber = yearNumber;
	}
	

}
